// pages/login/login.js
Page({
  data: {
    inputValues: [
      "", "", "", "", "", ""
    ],
    isFocus: true,
    password: ''
  },

  onLoad: function(option) {

  },

  setFocus: function() {
    this.setData({
      isFocus: true
    })
  },
  setPassword: function(e) {
    this.setData({
      password: e.detail.value
    })
    var password = e.detail.value.split('')
    if (e.detail.value.length > 0) {
      for(var i = 0; i < 6; i++) {
        if(!password[i]){
          password[i] = ""
        }
      }
      this.setData({
        inputValues: password
      })
    } else {
      this.setData({
        inputValues: [
          "", "", "", "", "", ""
        ]
      })
    }
    if (e.detail.value.length == 6) {
      this.login()
    }
  },
  login: function() {
    wx.showLoading({
      title: '验证中',
    })
    this.setData({
      isFocus: false
    })
    var that = this
    wx.cloud.callFunction({
      name: 'getAdminByOpenid',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        wx.hideLoading();
        if(res.result.data.length == 0) {
          wx.showModal({
            title: '警告',
            content: '你不是管理员，没有权限访问',
            showCancel: false,
            success: function(res) {
              if(res.confirm === true) {
                wx.navigateBack({})
              }
            }
          })
        } else {
          if(res.result.data[0].password == that.data.password) {
            wx.showToast({
              title: '验证通过',
              success: function() {
                wx.redirectTo({
                  url: '../manageExam/manageExam',
                })
              }
            })
          } else {
            wx.showToast({
              title: '验证密码错误',
              icon: 'none',
              success: function() {
                that.setData({
                  isFocus: true,
                  password: '',
                  inputValues: [
                    "", "", "", "", "", ""
                  ]
                })
              }
            })
          }
        }
      }
    })
  }
})